<?php

namespace app\advertiser\controller;

use app\admin\model\advertiser\AdvertiserMatter;
use app\admin\model\advertiser\AdvertiserTask;
use app\advertiser\service\AdMatterService;
use app\BaseController;
use think\facade\Request;
use think\facade\View;
use think\response\Json;
use wechat\WsOpenPlatform;

/**
 * 微信公众号推广物
 */
class AdOfficialAccount extends BaseController
{
    /**
     * 公众号类型
     * @var int
     */
    private int $type = 1;

    /**
     * 公众号推广物列表
     * @param AdMatterService $adMatterService
     * @return Json
     */
    public function list(AdMatterService $adMatterService): Json
    {
        $params['page'] = Request::param('page/d', 1);
        $params['limit'] = Request::param('limit/d', 10);
        $params['name'] = Request::param('name/s');
        $params['start_time'] = Request::param('start_time/d', 0);
        $params['end_time'] = Request::param('end_time/d', 0);
        return $adMatterService->list($params, $this->type);
    }

    /**
     * 公众号推广物全部列表
     * @param AdMatterService $adMatterService
     * @return Json
     */
    public function listAll(AdMatterService $adMatterService): Json
    {
        $params['name'] = Request::param('name/s');
        return $adMatterService->listAll($params, $this->type);
    }

    /**
     * 公众号推广物增加
     * @param AdMatterService $adMatterService
     * @return Json
     */
    public function add(AdMatterService $adMatterService): Json
    {
        $params['name'] = Request::param('name/s', '');
        $params['appid'] = Request::param('appid/s', '');
        $params['desc'] = Request::param('desc/s', '');
        $params['avatar'] = Request::param('avatar/s', '');
        $params['qrcode'] = Request::param('qrcode/s', '');
        return $adMatterService->add($params, $this->type);
    }

    /**
     * 公众号推广物编辑
     * @param AdMatterService $adMatterService
     * @return Json
     */
    public function edit(AdMatterService $adMatterService): Json
    {
        $params['id'] = Request::param('id/d');
        $params['name'] = Request::param('name/s', '');
        $params['desc'] = Request::param('desc/s', '');
        $params['avatar'] = Request::param('avatar/s', '');
        $params['qrcode'] = Request::param('qrcode/s', '');
        return $adMatterService->edit($params, $this->type);
    }

    /**
     * 公众号推广物删除
     * @param AdMatterService $adMatterService
     * @return Json
     */
    public function del(AdMatterService $adMatterService): Json
    {
        $id = Request::param('id/d', 0);
        return $adMatterService->del($id, $this->type);
    }

    /**
     * 公众号一键配置
     * @return string
     */
    public function oaThirdAuth(): string
    {
        $appid = Request::param('appid/s', '');
        $Authorization = Request::param('Authorization/s', '');
        $authorizeUrl = '';
        $user_id = WS_AD_USER_ID;
        View::assign('code', 0);
        View::assign('authorizeUrl', $authorizeUrl);
        //判断当前appid是否存在
        $where1 = ['appid' => $appid, 'ad_user_id' => $user_id, 'closed' => 0];
        $res = AdvertiserMatter::findOne($where1, 'id');
        if (!$res) {
            View::assign('code', 2);
            return View::fetch('ad_official_account/oa_third_auth');
        }
        //判断是否存在审核通过或者已发布的推广任务
        $where2[] = ['matter_id', '=', $res['id']];
        $where2[] = ['ad_user_id', '=', $user_id];
        $where2[] = ['status', 'in', [4, 5]];
        $where2[] = ['closed', '=', 0];
        $res = AdvertiserTask::findOne($where2, 'id');
        if ($res) {
            View::assign('code', 3);
            return View::fetch('ad_official_account/oa_third_auth');
        }
        //获取当前域名
        $domain = $this->request->domain();
        $redirect_uri = $domain . '/advertiser/oaThirdAuth2?appid=' . $appid . '&Authorization=' . $Authorization;
        $Open = new WsOpenPlatform();
        $authorizeUrl = $Open->getPreAuthorizationUrl($redirect_uri);
        View::assign('code', 1);
        View::assign('authorizeUrl', $authorizeUrl);
        return View::fetch('ad_official_account/oa_third_auth');
    }

    /**
     * 公众号一键配置回调
     * @return string
     */
    public function oaThirdAuth2(): string
    {
        //接收appid
        $appid = Request::param('appid/s', '');
        $user_id = WS_AD_USER_ID;
        $bool = false;
        //查找当前模型
        $where = ['appid' => $appid, 'ad_user_id' => $user_id, 'closed' => 0];
        $Model = AdvertiserMatter::where($where)->find();
        if ($Model) {
            //接收返回值
            $Open = new WsOpenPlatform();
            $authorizeUrl = $Open->handleAuthorize();
            if (isset($authorizeUrl['authorization_info'])) {
                //获取授权的APPID
                $authorizer_appid = $authorizeUrl['authorization_info']['authorizer_appid'];
                if ($appid == $authorizer_appid) {
                    $data['auth'] = 1;
                    $data['auth_info'] = json_encode($authorizeUrl['authorization_info']);
                    $data['auth_time'] = time();
                    $bool = $Model->save($data);
                }
            }
        }
        View::assign('bool', $bool);
        return View::fetch('ad_official_account/oa_third_auth2');
    }

    /**
     * 获取授权方账号基本信息
     * @return Json
     */
    public function adWechatAuthorizer(): Json
    {
        $appid = Request::param('appid/s', '');
        $Open = new WsOpenPlatform();
        $info = $Open->getAuthorizer($appid);
        return jsonReturn(1, $info);
    }

    /**
     * 获取已授权的授权方列表
     * @return Json
     */
    public function adWechatAuthList(): Json
    {
        $offset = Request::param('offset/d', 0);
        $count = Request::param('count/d', 500);
        $Open = new WsOpenPlatform();
        $list = $Open->getAuthorizers($offset, $count);
        return jsonReturn(1, $list);
    }


}